<?php

/*
 * Form
 */

class   Form{
    
    private $_fields=array();
    private $_attributes = array();
    private $_target;
    private $_method='GET';
    private $_class;
    private $_id;
    private $_name;
    private $_enctype;
    private $_common_error;
    private $_submit_flag = false;
    
    public  function    __construct($_name, $_class, $_id, $_enctype){
        
        $this->_name    =   ' name="'.$_name.'"';
        $this->_class   =   ' class="'.$_class.'"';
        $this->_id      =   ' id="'.$_id.'"';
        $this->_enctype =   ' enctype="'.$_enctype.'"';
        
    }

    public function setTarget($_target){ // Set the target URL
        
        $this->_target  =   $_target;
    }
    
    public function getTarget(){
        
        return $this->_target;
    }
    
    public function setMethod($_method){    // GET or POST
        
        $this->_method  =   $_method;
        
    }
    
    public function getMethod(){
        
        return $this->_method;
        
    }

    public function addField(FormField  $_instance){
        
        $this->_fields[$_instance->getName()] = $_instance;
        
    }
    
    public function getField($name){
        
        return $this->_fields[$name];
    }
    
    public function getFields(){
        return $this->_fields;
    }
    
    public function setCommonError($_error){
        
        $this->_common_error = $_error;
    }

    public function setFieldValue($key, $value){
        
        $field = $this->_fields[$key];
        $field->setValue($value);
        $field_html_handler = $field->getHTMLHandler();
        $field_html_handler->setDisplayValue($this->getFieldValue($key));
        
    }
    
    public function clearFieldValue($key){
        
        $field = $this -> _fields[$key];
        $field -> setValue("");
        $field_html_handler = $field->getHTMLHandler();
        $field_html_handler->setDisplayValue($this->getFieldValue($key));
        
    }
    
    public function getFieldValue($key){
        
        $field = $this->_fields[$key];
        
        if(isset($field)){
            return $field->getValue();
        }
        
        else return false;
    }

    public function handleSubmit($args){

            $param_array = $args;
            $submit_flag = false;

            foreach($this->_fields as $field){
                    if(isset($param_array[$field -> getName()])){
                     $this -> setFieldValue($field -> getName(),$param_array[$field -> getName()]);
                     $this ->_attributes[$field->getName()] = $param_array[$field->getName()];
                     $field_flag = true;
                    }
                    else{
                     $field_flag = false;
                    }
                    $submit_flag = $submit_flag || $field_flag;
                    $this -> _submit_flag = $submit_flag;
            }
            return $submit_flag;
    }
    
    public function isSubmitted(){
        
        return $this -> _submit_flag;
        
    }

    public function validate(){
        
        $flag = true;
        
        foreach ($this->_fields as $field) {
            
           $itr_flag = $field->validate();
           $flag = $flag && $itr_flag;
        
        }

        return $flag;
    }
    
    public function getAttributes(){
        return $this -> _attributes;
    }

    public function renderAsTable(){
            
       //   Render the form in a table
        
       $row_content = array();
//       $data_content = array();
       $form_start = null;
       $table_start = null;
       
       $form_start = '<form'.$this->_enctype.$this->_class.$this->_id.$this->_name.'  method="'.$this->getMethod().'" action="'.$this->getTarget().'">';
       $table_start = '<table'.$this->_class.$this->_id.$this->_name.' border="1">';
       
       foreach ($this->_fields as $field) {
           
           array_push($row_content,'<tr>');
           array_push($row_content,'<td>'.$field->getHTMLHandler()->getLabel().'</td>');
           array_push($row_content,'<td>'.$field->getHTMLHandler()->render().'</td>');
           
            // Output the error to screen in case there is an error in individual fields
           if($field->getDefaultError()){
               
               array_push($row_content,'<td>'.$field->getDefaultError().'</td>');
           }
           array_push($row_content,'</tr>');
       }
       
       
       if(isset($this->_common_error)){
               array_push($row_content,'<tr><td>Error : </td><td>'.$this->_common_error.'</td></tr>');
           }
       array_push($row_content,'<tr><td></td><td><input type="submit" value="Submit" /></td></tr>');
       $table_end = '</table>';
       $form_end = '</form>';
       
       $string = $form_start.$table_start;
       
       foreach($row_content as $row){
           
           $substr .= $row;
           
       }
       
       $string = $string.$substr.$table_end.$form_end;
       
       return $string;
       
    }
    
    public function renderAsList(){
        
        echo '<form'.$this->_enctype.$this->_class.$this->_id.$this->_name.'  method="'.$this->getMethod().'" action="'.$this->getTarget().'>';
        echo '<ul>';
            foreach ($this->_fields as $field) {
                echo '<li>'.$field->getHTMLHandler()->getLabel().$field->render().'</li>';
            }
            if(isset($this->_common_error)){
              echo  '<li>Error : </li><li>'.$this->_common_error.'</li>';
            }
        echo    '<li><input type="submit" value="Submit" /></li>';
        echo '</ul>';
        
    }
    
    public function renderAsDiv(){
        
        echo    '<div '.$this->_class.$this->_id.$this->_name.'>';
         echo '<form'.$this->_enctype.$this->_class.$this->_id.$this->_name.'  method="'.$this->getMethod().'" action="'.$this->getTarget().'">';
         foreach ($this->_fields as $field) {
                echo '<span>'.$field->getHTMLHandler()->getLabel().$field->render().'</span><br/>';
            }
         echo   '<input type="submit" value="Submit" />';
         echo '</form>';
         echo '</div>';
    } 
}
?>
